package com.jpyl.jpMedicalTreatment.view.adapter;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.TextView;

import com.jpyl.jpMedicalTreatment.R;
import com.jpyl.jpMedicalTreatment.network.Const;

import java.util.List;

/**
 * 首页列表适配器
 * Created by 15697 on 2019/1/4.
 */

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> implements View
        .OnClickListener {
    Context context;
    private List<String> list;

    public HomeAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_item_home,
                null);
        view.setOnClickListener(this);
        return new HomeAdapter.ViewHolder(view);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        holder.tv_money.setVisibility(View.GONE);
        holder.tv_name.setText(list.get(position));
        holder.tv_name.setTextColor(context.getColor(R.color.titleBg));
        holder.tv_money.setText(context.getString(R.string.money) + Const.MONEY);
        holder.itemView.setTag(position);
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public void onClick(View view) {
        if (mItemClickListener != null) {
            mItemClickListener.onItemClick((Integer) view.getTag());
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_name, tv_money;

        ViewHolder(View convertView) {
            super(convertView);
            tv_name = convertView.findViewById(R.id.tv_itemHome_name);
            tv_money = convertView.findViewById(R.id.tv_itemHome_money);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    private OnItemClickListener mItemClickListener;

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

}
